package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import quiz.SimpleQuiz;

public class GUIMain extends JFrame {
	
	private static final long serialVersionUID = 1L;
	JButton createQuiz;
	JButton saveQuiz;
	JButton loadQuiz;
	JButton printBasicSetup;
	JButton printHelper;
	JButton printSolution;
	JPanel theButtons;
	JScrollPane scrollDisplayArea;
	JTextArea displayArea;
	ButtonActionListener buttonActionListener;
	
	SimpleQuiz quiz;

	public GUIMain() {
		initializeComponents();
		configurateWindows();
		setupComponents();
		connectComponents();
		addActionListeners();
		
	}
	
	private void configurateWindows() {
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setPreferredSize(new Dimension(450,300));
		setLocation(new Point(550,250));
		pack();
		setVisible(true);
		
		theButtons.setLayout(new GridLayout(6,1));
//		scrollDisplayArea.setLayout(new FlowLayout());
	}

	private void initializeComponents() {
		
		quiz = new SimpleQuiz();
		buttonActionListener = new ButtonActionListener(this);
		createQuiz = new JButton("Opprett quiz!");
		saveQuiz = new JButton("Lagre quiz");
		loadQuiz = new JButton("Last inn quiz");
		printBasicSetup = new JButton("Print oppgaver");
		printHelper = new JButton("Print hjelp for oppgavemaker");
		printSolution = new JButton("Print svar");
		theButtons = new JPanel();
		displayArea = new JTextArea("");
		
		scrollDisplayArea = new JScrollPane(displayArea);
	}

	private void setupComponents() {
		displayArea.setPreferredSize(new Dimension(200,250));
		saveQuiz.setEnabled(false);
	}

	private void connectComponents() {
		theButtons.add(createQuiz);
		theButtons.add(saveQuiz);
		theButtons.add(loadQuiz);
		theButtons.add(printBasicSetup);
		theButtons.add(printHelper);
		theButtons.add(printSolution);
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(theButtons, BorderLayout.WEST);
		getContentPane().add(scrollDisplayArea, BorderLayout.EAST);
		
	}

	private void addActionListeners() {
		createQuiz.addActionListener(buttonActionListener);
		saveQuiz.addActionListener(buttonActionListener);
		loadQuiz.addActionListener(buttonActionListener);
		printBasicSetup.addActionListener(buttonActionListener);
		printHelper.addActionListener(buttonActionListener);
		printSolution.addActionListener(buttonActionListener);
	}

	public static void main(String[] args) {
		new GUIMain();
		
	}
}

